package main

func countServers(grid [][]int) int {
	//记录每行每列服务器数
	rowNumMap := make(map[int]int)
	colNumMap := make(map[int]int)

	M, N := len(grid), len(grid[0])
	for i := 0; i < M; i++ {
		for j := 0; j < N; j++ {
			if grid[i][j] == 1 {
				rowNumMap[i] += 1
				colNumMap[j] += 1
			}
		}
	}

	//再次遍历，判断每个服务器是否可以和同行同列其他服务器通信，若可以通信结果+1
	res := 0
	for i := 0; i < M; i++ {
		for j := 0; j < N; j++ {
			if grid[i][j] == 1 && (rowNumMap[i] > 1 || colNumMap[j] > 1) {
				res++
			}
		}
	}
	return res
}
